#include <earthworm.h>
#include <transport.h>
#include <trace_buf.h>
#include <comedilib.h>

#ifndef _ADSEND_COMEDI_
#define _ADSEND_COMEDI_
#define TD_LEN  500

typedef struct {
   char sta[6];
   char comp[4];
   char net[3];
   int  pin;
} SCN;

typedef struct {
  SCN* scn;
  unsigned int subdevice;
  unsigned int channel;
  unsigned int range;
  unsigned int aref;
} ChannelInfo;

typedef struct {
  char *Device;
  ChannelInfo *ChannelList;
  int ChannelListLength;
  int SamplesPerChannel;
  unsigned int PeriodNs;
  int SamplesPerSecond;
  int ReadBufferSize; //This is the size of the buffer that the daq card 
                      //outputs too
                      //it must be large enough for the samples/second so 
                      //that we don't
                      //run out of room and overflow.
  long outKey; //we might not need this...use OutputRing instead
  char *ModuleId;
  char *OutputRing;
  char *InstallationId;
  MSG_LOGO msg_logo;
} ew_adsend_config_file;

/*typedef struct {
	int numSamples;
	int chanlistIndex;
	int *data;
	struct timeval *startTime;
	struct timeval *endTime;
	} TraceDataInfo;*/

typedef struct {
    TRACE_HEADER *queue[TD_LEN];
    int first;
    int last;
    int count;
} TraceDataQueue;

typedef struct {
    comedi_t *dev; //handle for the DAQ card
    comedi_cmd *command; //the COMEDI command, set in initialize_daq()
    unsigned int *channelList;
    ew_adsend_config_file *configfile;
    int subdevice;
    void *map;
    //stuff for streaming buffer
    int streaming_buffer_size;
    int front;
    int back;
    int current_chan;
    int stop_flag;
    TraceDataQueue *trace_queue;
} daq_handle;

void get_results_thread(daq_handle *handle);
void earthworm_packager_thread(daq_handle *handle);
TRACE_HEADER *get_next_blank_trace_header(TraceDataQueue *trace_queue);
void next_trace_header_ready(TraceDataQueue *trace_queue);
TRACE_HEADER *pop_trace_data(TraceDataQueue *trace_queue);
void usage();

#endif
